
/* ------------------------------------------------------------------------
アコーディオン
	Copyright (c) 2009, Matsumoto.JS All rights reserved.

【使用方法】（なるべく<head>タグ内にて）
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js?load=effects" type="text/javascript"></script>
<script src="js/accordion.js" type="text/javascript"></script>

【パラメータの説明】
オブジェクトリテラルで指定
	container: 対象のブロック（default:"accordion"）
	toggle: 個別に開閉をトグル（default:"false"）

HTML: 
<!-- 構造内のCSSクラス "accordion-XXXX" は必須です -->
<!-- "accordion-body" にpadding指定をすると動きが荒れるようです -->
<div id="accordion">
	<div class="accordion-content">
		<div class="accordion-head accordion-open">Title</div>
		<div class="accordion-body">Body</div>
	</div>
	<div class="accordion-content">
		<div class="accordion-head">Title</div>
		<div class="accordion-body">Body</div>
	</div>
</div>
<script language="JavaScript" type="text/javascript">
	Event.observe(window,'load',function(){new Accordion();});
</script>
 ------------------------------------------------------------------------ */

var Accordion = Class.create();
Accordion.prototype = {
initialize: function(option) {
	var props = { container : "accordion", toggle : false };
	if(option) for(var key in option) props[key] = option[key];
	this.container = $(props.container);
	if (!this.container) return;
	if (props.toggle) {
		this.container.select('div.accordion-content').each(
		function(el){
			var el_head = el.down('.accordion-head');
			var el_body = el.down('.accordion-body');
			el_head.observe("click",function(){
				if (el_head.hasClassName('accordion-open')) {
					new Effect.BlindUp(el_body, { duration:0.1 });
					el_head.removeClassName('accordion-open');
				} else {
					new Effect.BlindDown(el_body, { duration:0.1 });
					el_head.addClassName('accordion-open');
				}	
			}.bindAsEventListener(this));
			if (!el_head.hasClassName('accordion-open')) el_body.style.display = 'none';
		}
		);
	} else {
		this.container.select('div.accordion-content').each(
		function(el){
			var el_head = el.down('.accordion-head');
			var el_body = el.down('.accordion-body');
			el_head.observe("click",function(){
				if (el_head.hasClassName('accordion-open')) return;
				el.up().select('div.accordion-body').each( function(elx){ 
					if (elx.visible() && elx != el_body) new Effect.BlindUp(elx, { duration:0.1 }); 
				});
				el.up().select('div.accordion-head').each( function(elx){ 
					if (elx.hasClassName('accordion-open')) elx.removeClassName('accordion-open');
				});
				new Effect.BlindDown(el_body, { duration:0.1 });
				el_head.addClassName('accordion-open');
			}.bindAsEventListener(this));
			if (!el_head.hasClassName('accordion-open')) el_body.style.display = 'none';
		}
		);
	}
}
}//prototype
